<template>
  <!-- 标签管理 -->
  <div class='container'>
    <el-card>
      <el-breadcrumb v-if="id" separator-class="el-icon-arrow-right" style="border-bottom: 1px solid #ebeef5;padding-bottom: 18px;margin-bottom:20px">
        <el-breadcrumb-item>学科管理</el-breadcrumb-item>
        <el-breadcrumb-item>{{name}}</el-breadcrumb-item>
        <el-breadcrumb-item>标签管理</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 搜索 -->
      <el-row>
        <el-col :span="18">
          <el-row>
            <el-form  label-width="80px">
            <el-col :span="8">
              <el-form-item label="标签名称" >
                <el-input v-model="tagName" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="状态">
                <el-select v-model="state" placeholder="请选择">
                  <el-option
                    v-for="item in option"
                    :key="item.label"
                    :label="item.value"
                    :value="item.label">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8" style="text-align: left">
              <el-button size="small" @click="clear">清除</el-button>
              <el-button type="primary" size="small" @click="search">搜索</el-button>
            </el-col>
          </el-form>
          </el-row>
        </el-col>
        <el-col :span="6" style="text-align: right">
          <el-button type="text" icon="el-icon-back" @click="$router.back()" v-if="id">返回学科</el-button>
          <el-button type="success" size="small" icon="el-icon-edit" @click="addTag">新增标签</el-button>
        </el-col>
      </el-row>
      <!-- 消息提示 -->
      <el-alert
        :closable="false"
        type="info"
        show-icon>
        <template slot="title">
          数据一共{{total}}条
        </template>
      </el-alert>
      <!-- 标签列表 -->
      <el-table
       :header-cell-style="{background:'#FAFAFA'}"
        fit
        highlight-current-row
      v-loading="loading"
      element-loading-text="给我一点点时间"
      :data="tagsList"
      style="width: 100%">
      <el-table-column
        type="index"
        label="序号"
        width="80">
      </el-table-column>
      <el-table-column
        prop="subjectName"
        label="所属学科"
        >
      </el-table-column>
      <el-table-column
        prop="tagName"
        label="标签名称"
        >
      </el-table-column>
      <el-table-column
        prop="username"
        label="创建者">
      </el-table-column>
      <el-table-column
        prop="addDate"
        label="创建日期">
        <template slot-scope="{row}">
          {{row.addDate | format}}
        </template>
      </el-table-column>
      <el-table-column
        prop="state"
        label="状态">
        <template slot-scope="{row}">
          {{row.state===1?'已启用':'已禁用'}}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        >
        <template slot-scope="{row}">
          <el-button
            type="text"
            size="small"
            @click="changeState(row)"
            >
            {{row.state===1?'禁用':'启用'}}
          </el-button>
          <el-button
            :disabled="row.state===1"
            type="text"
            size="small"
            @click="showUpdate(row)"
            >
            修改
          </el-button>
          <el-button
            :disabled="row.state===1"
            type="text"
            size="small"
            @click="deleteTag(row)"
            >
            删除
          </el-button>
        </template>
      </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row style="text-align: right">
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="tagsForm.page"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="tagsForm.pagesize"
        background
        layout="  prev, pager, next,sizes, jumper"
        :total="total">
        </el-pagination>
      </el-row>
    </el-card>
    <!-- 修改弹层 -->
    <el-dialog
      :title="updateForm.id?'修改标签':'新增标签'"
      :visible="showUpdateDialogVisible"
      @close='close'
      width="30%">
      <el-form ref="updateForm"  label-width="80px" :model="updateForm">
        <el-form-item label="所属学科" v-if="!id">
          <el-select style="width:100%" v-model="updateForm.subjectID" placeholder="请选择">
            <el-option
              v-for="item in subjectsSimpleList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="标签名称"
          prop="tagName"
          :rules="[
            { required: true, message: '标签名称不能为空'},
          ]"
         >
          <el-input v-model="updateForm.tagName" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="updateSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, changeState, update, remove, add } from '@/api/hmmm/tags.js'
import { simple } from '@/api/hmmm/subjects.js'
export default {
  name: 'tags',
  data () {
    return {
      tagName: '',
      state: '',
      option: [{
        value: '启用',
        label: 1
      }, {
        value: '禁用',
        label: 0
      }], // 状态列表
      tagsForm: {
        page: 1, // 当前在第几页
        pagesize: 10// 每页显示条数
      },
      tagsList: [], // 标签列表
      total: 0, // 总条数
      showUpdateDialogVisible: false,
      updateForm: {
        id: '',
        subjectID: '',
        tagName: ''
      },
      subjectsSimpleList: [],
      loading: true,
      id: this.$route.query.id, // 路由传过来的id
      name: this.$route.query.name // 路由传过来的name
    }
  },
  created () {
    this.loadTagsList()
    this.getSubjectsSimpleList()
  },
  // watch: {
  //   $route: {
  //     handler () {
  //       this.id = this.$route.query.id
  //     }
  //   }
  // },
  methods: {
    // 获取标签列表
    async loadTagsList () {
      if (this.id) {
        this.tagsForm.subjectID = this.id
      }
      if (this.state !== '') {
        this.tagsForm.state = this.state
      }
      if (this.tagName !== '') {
        this.tagsForm.tagName = this.tagName
      } else {
        delete this.tagsForm.tagName
      }
      try {
        const res = await list(this.tagsForm)
        this.loading = false
        this.tagsList = res.data.items
        // console.log(res.data)
        this.total = res.data.counts
      } catch (err) {
        console.log(err)
      }
    },
    // 每页条数改变触发
    handleSizeChange (val) {
      this.tagsForm.pagesize = val
      this.loadTagsList()
    },
    // 当前页改变触发
    handleCurrentChange (val) {
      this.tagsForm.page = val
      this.loadTagsList()
    },
    // 清除
    clear () {
      if (this.tagName !== '' || this.state !== '') {
        this.tagName = ''
        this.state = ''
        this.tagsForm = {
          page: 1, // 当前在第几页
          pagesize: 10// 每页显示条数
        }
        this.loadTagsList()
      }
    },
    search () {
      this.loadTagsList()
    },
    // 修改状态
    async changeState (row) {
      // console.log(row)
      let state = 1
      if (row.state === 1) {
        state = 0
      }
      await changeState({
        id: row.id,
        state
      })
      this.$message.success('操作成功')
      this.loadTagsList()
    },
    // 修改标签
    showUpdate (row) {
      // console.log(row)
      this.showUpdateDialogVisible = true
      this.updateForm.subjectID = row.subjectID
      this.updateForm.id = row.id
      this.updateForm.tagName = row.tagName
    },
    // 获取学科简单列表
    async getSubjectsSimpleList () {
      const res = await simple()
      // console.log(res)
      this.subjectsSimpleList = res.data
    },
    // 修改，添加标签确定
    async updateSave () {
      try {
        if (this.id) {
          this.updateForm.subjectID = this.id - 0
        }
        if (this.updateForm.id === '') {
          await add(this.updateForm)
          this.$message.success('添加成功')
        } else {
          await update(this.updateForm)
          this.$message.success('修改成功')
        }
      } catch (err) {
        console.log(err)
      }
      this.loadTagsList()
      this.close()
    },
    // 删除标签
    async deleteTag (row) {
      this.$confirm('此操作将永久删除该标签, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          if (this.tagsList.length === 1 && this.tagsForm.page !== 1) {
            this.tagsForm.page--
          }
          await remove({
            id: row.id
          })
          this.$message.success('删除成功')
          this.loadTagsList()
        } catch (err) {
          console.log(err)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async addTag () {
      this.showUpdateDialogVisible = true
    },
    // 关闭弹层
    close () {
      this.showUpdateDialogVisible = false
      this.updateForm = {
        id: '',
        subjectID: '',
        tagName: ''
      }
      this.$refs.updateForm.resetFields()
    }
  }
}
</script>

<style scoped lang='less'>
.container{
  padding: 10px;
  .el-table{
    margin: 15px 0;

  }
}

</style>
